package com.wyz.activemq.topic;

import com.wyz.activemq.config.AppConfig;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author: wyz
 * @Date: 2019/4/12 23:09
 * @Description:
 */
public class TopicProducer {

    public static void main(String[] args) throws JMSException {
        // 1.创建ActiveMQFactory
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(AppConfig.ACTIVEMQ_URL);
        //2.创建连接
        Connection connection = factory.createConnection();

        connection.start();
        //3.创建回话 第一个参数是是否开启事务 第二个参数是签收机制  AUTO_ACKNOWLEDGE 表示自动签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建Topic队列
        Topic topic = session.createTopic(AppConfig.TOPIC_QUEUE_NAME);
        MessageProducer producer = session.createProducer(topic);
        for (int i = 0; i < 10; i++) {
            TextMessage textMessage = session.createTextMessage("Topic发送消息===" + i);
            producer.send(textMessage);
        }
        System.out.println("Topic模式发送消息完成！！！");
        connection.close();
    }
}
